home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
MacWorld 1999 July
/
Macworld (1999-07).dmg
/
Serious Software
/
OpenWorld demo 2.0
/
Development
/
SingleTag
/
Counter
/
Counter.c
next >
Wrap
Text File
|
1999-04-28
|
4KB
|
216 lines
/* File: Counter.c
Copyright (c) 1999 Marco Bambini. All rights reserved.
Description: page counter
*/
#include "Counter.h"
// global variables
static long times=0;
Boolean main (dataPtr param)
{
switch (param->message)
{
case init_message:
{
FILE *f;
HeadQueue *q;
// create the queue
create_queue(q);
if (q==NULL) return NULL;
// initialize fields
param->version=kVersion;
param->customData=(long)q;
// try to open the Counter file
f=fopen(kFileName,"r");
if (f)
{ // the file exists
char line[kMaxLine]; // max characters in a line
int max=kMaxLine;
char *tok;
long n;
// read the file (one line at a time) and add it to the list
while (fgets(line,max,f))
{
tok=strtok(line,"\t ");
n=atol(strtok(NULL,"\n "));
my_add_queue(param,tok,n);
}
}
else
{
// create the file
f=fopen(kFileName,"w");
}
// close the file
fclose(f);
}
break;
case run_message:
{
obj *item;
long l;
if (param->search==NULL) return NULL;
if (param->customData==NULL) return NULL;
item=find_queue((HeadQueue *)param->customData,param->search);
if (item)
{ // there is the item in the list, so return its value+1
item->value+=1;
l=item->value;
}
else
{ // new item, add to the list and return 1
l=1;
my_add_queue(param,param->search,l);
}
// prepare the output
param->outputText=(char *)OW_NewPtr(param,12);
if (param->outputText) param->dim=sprintf(param->outputText,"%ld",l);
}
break;
case stop_message:
{
// dispose the buffer
OW_DisposePtr(param,param->outputText);
// increment the global counter
++times;
// check if I have to write the list to the file
if (times==kMaxTimes) {WriteListToFile((HeadQueue *)param->customData);times=0;}
}
break;
case end_message:
{
// write the list to the file
WriteListToFile((HeadQueue *)param->customData);
// delete the queue
delete_queue(param,(HeadQueue *)param->customData);
}
break;
}
return kSingleTag;
}
void WriteListToFile(HeadQueue *q)
{
FILE *f;
QueueEl *now;
char line[kMaxLine]; // max characters in a line
// open the file
f=fopen(kFileName,"w");
if (f==NULL) return;
now=q->first;
while (now!=NULL)
{
sprintf(line,"%s\t%ld\n",now->item->page,now->item->value);
fputs(line,f);
now=now->next;
}
// close the file
fclose(f);
}
void my_add_queue(dataPtr param,char *s,long l)
{
obj *item=NULL;
// create a new object
item=(obj *)OW_NewPtr(param,sizeof(obj));
if (item==NULL) return;
// set its fields
item->page=(char *)OW_NewPtr(param,strlen(s)+1);
if (item->page) strcpy(item->page,s);
item->value=l;
add_queue(param,(HeadQueue *)param->customData,item);
}
/**************************************************************/
/* File: llist.c (linked list routines)
Copyright (c) 1997-99 Marco Bambini. All rights reserved.
Description:
Linked list manager.
Revision history:
970613 Created. (MB)
981020 revised to use with WSAPI
990427 revised to use with OpenWorld API
*/
/**************************************************************/
void add_queue(dataPtr param,HeadQueue *phq, void *item)
{
QueueEl *pqe;
pqe=(QueueEl *)OW_NewPtr(param,sizeof(QueueEl));
pqe->next=NULL;
pqe->item=item;
if (phq->cont==0)
{
phq->first=pqe;
phq->last=pqe;
}
else
{
phq->last->next=pqe;
phq->last=pqe;
}
phq->cont=phq->cont+1;
}
void delete_queue(dataPtr param,HeadQueue *phq)
{
QueueEl *prec,*now;
now=phq->first;
while (now!=NULL)
{
prec=now->next;
if (now->item->page) OW_DisposePtr(param,(Ptr)now->item->page);
if (now->item) OW_DisposePtr(param,(Ptr)now->item);
if (now) OW_DisposePtr(param,(Ptr)now);
now=prec;
}
phq->cont=0;
}
obj *find_queue(HeadQueue *phq,char *tag)
{
QueueEl *now;
obj *last=NULL;
now=phq->first;
while (now!=NULL)
{
if (up_strcmp(now->item->page,tag)==NULL) return now->item;
now=now->next;
}// while (now!=NULL)
return NULL;
}